#This file contains the following recoding for the core :
# 1) Descriptive statistics (Appendix Section A)

# 0) Data preparation ####

dat <- readRDS("data/data.rds")

dat <- dplyr::left_join(dat, 
                        dat %>% 
                          group_by(country_short) %>% 
                          summarise(sd_cntry_tba = sd(stc_tba_change, na.rm=T),
                                    me_cntry_tba = mean(stc_tba_change, na.rm=T),
                                    sd_cntry_sym = sd(stc_sym_change, na.rm=T),
                                    me_cntry_sym = mean(stc_sym_change, na.rm=T),
                                    sd_cntry_add = sd(stc_add_change, na.rm=T),
                                    me_cntry_add = mean(stc_add_change, na.rm=T),
                                    sd_cntry_net = sd(stc_net_change, na.rm=T),
                                    me_cntry_net = mean(stc_net_change, na.rm=T)),
                        by = "country_short") %>% 
  mutate(stc_tba_change_bin = factor(ifelse(stc_tba_change < me_cntry_tba-.5*sd_cntry_tba, "Decrease",
                                            ifelse(stc_tba_change < me_cntry_tba+.5*sd_cntry_tba, "Increase",
                                                   ifelse(is.na(dat$stc_tba_change), NA, "Stable"))),
                                     levels = c("Decrease", "Stable", "Increase")),
         stc_sym_change_bin = factor(ifelse(stc_sym_change < me_cntry_sym-.5*sd_cntry_sym, "Decrease",
                                            ifelse(stc_sym_change < me_cntry_sym+.5*sd_cntry_sym, "Increase",
                                                   ifelse(is.na(dat$stc_sym_change), NA, "Stable"))),
                                     levels = c("Decrease", "Stable", "Increase")),
         stc_add_change_bin = factor(ifelse(stc_add_change < me_cntry_add-.5*sd_cntry_add, "Decrease",
                                            ifelse(stc_add_change < me_cntry_add+.5*sd_cntry_add, "Increase",
                                                   ifelse(is.na(dat$stc_add_change), NA, "Stable"))),
                                     levels = c("Decrease", "Stable", "Increase")),
         stc_net_change_bin = factor(ifelse(stc_net_change < me_cntry_net-.5*sd_cntry_net, "Decrease",
                                            ifelse(stc_net_change < me_cntry_net+.5*sd_cntry_net, "Increase",
                                                   ifelse(is.na(dat$stc_net_change), NA, "Stable"))),
                                     levels = c("Decrease", "Stable", "Increase")))

dat$lr_cat <- factor(ifelse(dat$party_econ < 2.5, "Far-Left",
                            ifelse(dat$party_econ < 4, "Left",
                                   ifelse(dat$party_econ < 6.1, "Center",
                                          ifelse(dat$party_econ < 7.6, "Right",
                                                 ifelse(is.na(dat$party_econ), NA, "Far-Right"))))),
                     levels = c("Far-Left", "Left", "Center", "Right", "Far-Right"))
